package ywpt.service.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.CallRecord;
import ywpt.model.ClientInfo;
import ywpt.model.ClientKeshi;
import ywpt.model.CommonTable;
import ywpt.model.Department;
import ywpt.model.JobInfo;
import ywpt.model.PeopleInfo;
import ywpt.web.common.PublicFunction;
@Service("jobInfoService")
@RemoteProxy(name="jobInfoService")
public class JobInfoServiceImpl extends HibernateEntityDao<JobInfo> implements JobInfoService{
	
	@Override
	@RemoteMethod
	public JobInfo getJobInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getJobInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<JobInfo> getJobInfos(DetachedCriteria query, int pageSize,
			int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public void removeJobInfo(Long id) {
		JobInfo jobInfo = this.get(id);
		jobInfo.setDeleteFlag(true);
		this.update(jobInfo);
	}

	@Override
	public void saveJobInfo(JobInfo jobInfo, Long callId, Long departId,
			Long typeId, Long clientId, Long keshiId, Long acceptManId,
			String[] assist,Long sendDepartId, Long sendBillId, Long proTypeId, Long stateId,
			Long imporDegId) {
		if(callId!=null){
			jobInfo.setCallRecord(this.get(CallRecord.class, callId));
		} 
		if(departId!=null) 
			jobInfo.setDepart(this.get(Department.class, departId));
		if(typeId!=null) 
			jobInfo.setType(this.get(CommonTable.class, typeId));
		if(clientId!=null) 
			jobInfo.setClient(this.get(ClientInfo.class, clientId));
		if(keshiId!=null) 
			jobInfo.setKeshi(this.get(ClientKeshi.class, keshiId));
		if(acceptManId!=null) 
			jobInfo.setAcceptMan(this.get(PeopleInfo.class, acceptManId));
		if(null!=assist&&assist.length>0){
			String str="";
			for(int i=0;i<assist.length;i++){
				str = str + assist[i]+",";
			}
			jobInfo.setAssistMans(str);
		}
		if(sendBillId!=null) 
			jobInfo.setSendBill(this.get(PeopleInfo.class, sendBillId));
		if(sendDepartId!=null)
			jobInfo.setSendDepart(this.get(Department.class, sendDepartId));
		if(proTypeId!=null) 
			jobInfo.setProType(this.get(CommonTable.class, proTypeId));
		if(stateId!=null) 
			jobInfo.setState(this.get(CommonTable.class, stateId));
		if(imporDegId!=null) 
			jobInfo.setImporDeg(this.get(CommonTable.class, imporDegId));
			
		
		this.save(jobInfo);
	}

	@Override
	public void updateJobInfo(JobInfo jobInfo, Long callId, Long departId,
			Long typeId, Long clientId, Long keshiId, Long acceptManId,
			String[] assist,Long sendDepartId, Long sendBillId, Long proTypeId, Long stateId,
			Long imporDegId) {
		if(callId!=null){
			jobInfo.setCallRecord(this.get(CallRecord.class, callId));
		}
		if(departId!=null) 
			jobInfo.setDepart(this.get(Department.class, departId));
		if(typeId!=null) 
			jobInfo.setType(this.get(CommonTable.class, typeId));
		if(clientId!=null) 
			jobInfo.setClient(this.get(ClientInfo.class, clientId));
		if(keshiId!=null) 
			jobInfo.setKeshi(this.get(ClientKeshi.class, keshiId));
		if(acceptManId!=null) 
			jobInfo.setAcceptMan(this.get(PeopleInfo.class, acceptManId));
		if(assist!=null&&assist.length>0){
			String str="";
			for(int i=0;i<assist.length;i++){
				str = str + assist[i]+",";
			}
			jobInfo.setAssistMans(str);
		}
		if(sendBillId!=null) 
			jobInfo.setSendBill(this.get(PeopleInfo.class, sendBillId));
		if(sendDepartId!=null)
			jobInfo.setSendDepart(this.get(Department.class, sendDepartId));
		if(proTypeId!=null) 
			jobInfo.setProType(this.get(CommonTable.class, proTypeId));
		if(stateId!=null) 
			jobInfo.setState(this.get(CommonTable.class, stateId));
		if(imporDegId!=null) 
			jobInfo.setImporDeg(this.get(CommonTable.class, imporDegId));		
		this.update(jobInfo);
	}

	@Override
	public String createJobCode(int mark) {
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		query.add(Restrictions.eq("mark", mark));
		String jobCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<JobInfo> jobInfos = this.getJobInfos(query);
		if ( jobInfos.iterator().hasNext() )	
		{
			code = jobInfos.iterator().next().getJobCode().substring(2);		
			maxId = new Integer(code);
		}
		jobCode=PublicFunction.getNextCode("", maxId, false,7); 
		if (mark==1) {
			return "RJ"+jobCode;
		} else {
			return "JS"+jobCode;
		}
		
	}
	

	@Override
	public String createSysCode(int mark) {
		SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMdd");
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		query.add(Restrictions.eq("mark", mark));
		query.add(Restrictions.like("sysCode",FORMAT.format(new Date())+"%"));
		String sysCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<JobInfo> jobInfos = this.getJobInfos(query);
		if ( jobInfos.iterator().hasNext() )	
		{
			code = (jobInfos.iterator().next().getSysCode()).substring(8);
			maxId = new Integer(code);
		}
		sysCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return FORMAT.format(new Date())+sysCode;
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<JobInfo> getJobInfos(DetachedCriteria query) {
		
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<JobInfo> getJobInfosByJobCode(String jobCode, int start,int size) {
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class,"client");
		query.add(Restrictions.eq("deleteFlag", false));
		if ( jobCode != null && !jobCode.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("jobCode", "%" + jobCode + "%"),  
							          Restrictions.like("jobCode", "%" + jobCode + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		List<JobInfo> jobInfoList = new ArrayList<JobInfo>();
		List<JobInfo> jobInfoLists = new ArrayList<JobInfo>();
		JobInfo jobInfo =new JobInfo();
		
		jobInfoList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<JobInfo> itr = jobInfoList.iterator(); itr.hasNext(); i++ ) 
		{
			jobInfo = itr.next();
			String ss = PublicFunction.format(jobInfo.getJobCode() + "",7, "  ", 1)
			          + ""; 
			
			jobInfo.setJobCode(ss);
			jobInfoLists.add(jobInfo);
		}
		return jobInfoLists;
	}

	@Override
	@RemoteMethod
	public int getJobInfosCountByJobCode(String jobCode) {
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		if ( jobCode != null && !jobCode.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("jobCode", "%" + jobCode + "%" ),  
							          Restrictions.like("jobCode", "%" + jobCode + "%" )));
				 //.add(Restrictions.eq("organType", "01"));
		}
		//query.addOrder(Order.asc("jobCode"));
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	@RemoteMethod
	public Boolean checkJobInfoExistByJobCode(String jobCode) {
		Boolean result = false; 
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		query.add(Restrictions.like("jobCode", "%"+jobCode+"%"));
		if (this.getCountByCriteria(query)>0) {
			result = true; 
		}
		return result;
	}

	@Override
	public boolean checkJobCode(String jobCode,int mark) {
		if (!"".equals(jobCode)) {
			DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
			query.add(Restrictions.eq("mark", mark));
			query.add(Restrictions.eq("deleteFlag",false));
			query.add(Restrictions.eq("jobCode", jobCode));
			int count=this.findAllByCriteria(query).size();
			if (count>0) {
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<JobInfo> getJobInfoByCallId(Long callId) {
		DetachedCriteria query = DetachedCriteria.forClass(JobInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		query.createAlias("callRecord", "callRecord").add(Restrictions.eq("callRecord.id", callId));
		return this.findAllByCriteria(query);
	}

 
	
	

}
